package com.qiaofei.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiaofei.mapper.NewHouseMapper;
import com.qiaofei.pojo.PageBean;
import com.qiaofei.pojo.NewHouse;
import com.qiaofei.service.NewHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class NewHouseServiceImpl extends BaseServiceImpl implements NewHouseService {

    @Autowired
    private NewHouseMapper newHouseMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, Map<String ,Object> params, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<NewHouse> newHouseList = newHouseMapper.list(params, begin, end);
        Page<NewHouse> p = (Page<NewHouse>) newHouseList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult(), p.getPages());
        return pageBean;
    }

    @Override
    public int add(NewHouse newHouse) {
        newHouse.setCreateTime(LocalDateTime.now());
        return newHouseMapper.insert(newHouse);
    }

    @Override
    public int update(NewHouse newHouse) {
        newHouse.setUpdateTime(LocalDateTime.now());
        return newHouseMapper.update(newHouse);
    }

    @Override
    public List<NewHouse> getByIds(List<Integer> ids) {
        List<NewHouse> newHouseList = newHouseMapper.getByIds(ids);
        return newHouseList;
    }

    @Override
    public int delete(List<Integer> ids) {
        List<NewHouse> newHouseList = getByIds(ids);
        List<String> headerList = new ArrayList<>();
        if(newHouseList.size() > 0){
            for (NewHouse newHouse :
                    newHouseList) {
                if(newHouse.getHouseImages().equals("") == false) {
                    headerList.add(newHouse.getHouseImages());
                }
            }
            deleteImages(headerList);
        }
        return newHouseMapper.delete(ids);
    }
}
